const http = require("http");
const request = require("./request");
const response = require("./response");
const context = require("./response");

class KKB {
  listen (...args) {
    const server = http.createServer((req, res) => {
      // 创建上下文
      const ctx = this.createContext(req, res);
      // this.callback(req, res);
      this.callback(ctx);
      res.end(ctx.body);
    });
    server.listen(...args);
  }
  use (callback) {
    this.callback = callback;
  }
  /**
   * 创建上下文
   * @param {*} req request
   * @param {*} res response
   */
  createContext (req, res) {
    const ctx = Object.create(context);
    ctx.request = Object.create(request);
    ctx.response = Object.create(response);

    ctx.req = ctx.request.req;
    ctx.res = ctx.response.res;
    return ctx;
  }
}

module.exports = KKB;